#include "base.h"

int main(int argc,char **argv)
{

       // Usage:
        //./rewrite_... -kappa_sea=0.161229 -conf=500 -Nmass=5 -Nmom=140 -Nr=2 -dir_input=ft/ -dir_output=s0ft/

  // Input
   char kappa_sea[1024];
   int conf;
   char dir_input[1024];
   char dir_output[1024];

   int Nr=2;
   int Nmass=5;
   int Nmom=140;
   sprintf(dir_input,"./ft/");
   sprintf(dir_output,"./s0ft-mom-m/");



   ////// Parsing command line

   for(int i=1; i<argc; i++) {
            if(strncmp(argv[i],"-kappa_sea",10)==0)  sscanf(argv[i],"-kappa_sea=%s",kappa_sea);
            if(strncmp(argv[i],"-conf",5)==0)  sscanf(argv[i],"-conf=%d",&conf);
            if(strncmp(argv[i],"-Nmass",6)==0)  sscanf(argv[i],"-Nmass=%d",&Nmass);
            if(strncmp(argv[i],"-Nmom",5)==0)  sscanf(argv[i],"-Nmom=%d",&Nmom);
            if(strncmp(argv[i],"-Nr",3)==0)  sscanf(argv[i],"-Nr=%d",&Nr);
            if(strncmp(argv[i],"-dir_input",10)==0)  sscanf(argv[i],"-dir_input=%s",dir_input);
            if(strncmp(argv[i],"-dir_output",11)==0)  sscanf(argv[i],"-dir_output=%s",dir_output);

   }



  check_endianess();
  if (big_endian) printf("ATENTION endianness will be transformed \n");


  // read original


  colorcolorspinspin ***FT=(colorcolorspinspin***)malloc(sizeof(colorcolorspinspin)*(Nmass));
  for (int imass=0; imass<Nmass; imass++) FT[imass]=(colorcolorspinspin**)malloc(sizeof(colorcolorspinspin)*(Nr));
  for (int imass=0; imass<Nmass; imass++) for (int ir=0; ir<Nr; ir++)  FT[imass][ir]=(colorcolorspinspin*)malloc(sizeof(colorcolorspinspin)*(Nmom));



  FILE *fin=NULL;
  char file_input[1024];
  sprintf(file_input,"%s/ft_%04d",dir_input,conf);
  fin=fopen(file_input,"rb+");
  if(fin == (FILE *)NULL) {
  fprintf(stderr,"Unable to open file %s for reading\n",file_input );
   return EXIT_FAILURE;
  }

 for (int ir=0; ir<Nr; ir++) for (int imass=0; imass<Nmass; imass++) for (int imom=0; imom<Nmom; imom++)
  {
                fread(&FT[imass][ir][imom],sizeof(colorcolorspinspin),1,fin);

                if(big_endian) {
                        for (int ic1=0; ic1<3; ic1++) for (int ic2=0; ic2<3; ic2++)  for (int is1=0; is1<4; is1++) for (int is2=0; is2<4; is2++) {
                        double FTr=creal(FT[imass][ir][imom][ic1][ic2][is1][is2]);
                        double FTi=cimag(FT[imass][ir][imom][ic1][ic2][is1][is2]);
                        doubles_to_doubles_changing_endianess(&FTr,&FTr,1);
                        doubles_to_doubles_changing_endianess(&FTi,&FTi,1);
                        FT[imass][ir][imom][ic1][ic2][is1][is2]=FTr+I*FTi;
                        }
                }
  }

   fclose(fin);



  FILE *fout=NULL;
  char file_output[1024];

  for (int ir=0; ir<Nr; ir++)
  {
         for(int imass=0; imass<Nmass; imass++)
         {
		for (int imom=0; imom<Nmom; imom++)
		{

	              sprintf(file_output,"%s/s0ft%d_%s_%04d_%03d_m%d.out",dir_output,ir,kappa_sea,conf,imom,imass);
	                   fout=fopen(file_output,"w");
        	           if(fout == (FILE *)NULL) {
                	        fprintf(stderr,"Unable to open file %s for writing\n",file_output);
                        	return EXIT_FAILURE;
                   	    }
				for (int ic1=0; ic1<3; ic1++) for (int ic2=0; ic2<3; ic2++)  for (int is1=0; is1<4; is1++) for (int is2=0; is2<4; is2++)	
		   		fwrite(&FT[imass][ir][imom][ic1][ic2][is1][is2],sizeof(complex),1,fout);
			fclose(fout);

		  }
	   }
      }

	


	return 0;
}
